{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "vscode": {
     "languageId": "shellscript"
    }
   },
   "outputs": [],
   "source": [
    "%%bash\n",
    "pip install -U byzerllm\n",
    "ray start --head"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "vscode": {
     "languageId": "shellscript"
    }
   },
   "outputs": [],
   "source": [
    "%%bash\n",
    "easy-byzerllm deploy deepseek-chat --token xxxxx --alias deepseek_chat"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "vscode": {
     "languageId": "shellscript"
    }
   },
   "outputs": [],
   "source": [
    "import byzerllm\n",
    "\n",
    "llm = byzerllm.ByzerLLM.from_default_model(model=\"deepseek_chat\")\n",
    "\n",
    "@byzerllm.prompt(llm = llm )\n",
    "def translate(content:str,lang:str) ->str:\n",
    "    '''\n",
    "    You are an expert in translation, \n",
    "    Translate the following from English to {{ lang }}:\n",
    "    {{ content }}\n",
    "    '''\n",
    "\n",
    "translate(\"hello!\",lang=\"Chinese\")    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import byzerllm\n",
    "\n",
    "llm = byzerllm.ByzerLLM.from_default_model(model=\"deepseek_chat\")\n",
    "\n",
    "@byzerllm.prompt(llm = llm )\n",
    "def hello(q:str) ->str:\n",
    "    '''\n",
    "    你好, {{ q }}\n",
    "    '''\n",
    "\n",
    "hello(\"你是谁\")    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "hello.prompt(\"你是谁\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "hello.with_llm(llm).run(\"你是谁\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "@byzerllm.prompt()\n",
    "def tell_story() -> str:\n",
    "    \"\"\"\n",
    "    讲一个100字的故事。    \n",
    "    \"\"\"\n",
    "s = (\n",
    "    tell_story.with_llm(llm)\n",
    "    .with_response_markers()\n",
    "    .options({\"llm_config\": {\"max_length\": 10}})\n",
    "    .run()\n",
    ")\n",
    "print(s)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "@byzerllm.prompt()\n",
    "def tell_story() -> str:\n",
    "    \"\"\"\n",
    "    讲一个100字的故事。    \n",
    "    \"\"\"\n",
    "s = (\n",
    "    tell_story.with_llm(llm)\n",
    "    .with_response_markers()    \n",
    "    .run()\n",
    ")\n",
    "print(s)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pydantic \n",
    "\n",
    "class Story(pydantic.BaseModel):\n",
    "    '''\n",
    "    故事\n",
    "    '''\n",
    "\n",
    "    title: str = pydantic.Field(description=\"故事的标题\")\n",
    "    body: str = pydantic.Field(description=\"故事主体\")\n",
    "\n",
    "@byzerllm.prompt()\n",
    "def tell_story()->Story:\n",
    "    '''\n",
    "    讲一个100字的故事。    \n",
    "    '''\n",
    "\n",
    "s = tell_story.with_llm(llm).run()\n",
    "print(isinstance(s, Story))\n",
    "print(s.title)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from loguru import logger\n",
    "import re\n",
    "\n",
    "@byzerllm.prompt()\n",
    "def generate_regex_pattern(desc: str) -> str:\n",
    "    \"\"\"\n",
    "    根据下面的描述生成一个正则表达式，要符合python re.compile 库的要求。\n",
    "\n",
    "    {{ desc }}\n",
    "\n",
    "    最后生成的正则表达式要在<REGEX></REGEX>标签对里。\n",
    "    \"\"\"    \n",
    "\n",
    "def extract_regex_pattern(regex_block: str) -> str:    \n",
    "    pattern = re.search(r\"<REGEX>(.*)</REGEX>\", regex_block, re.DOTALL)\n",
    "    if pattern is None:\n",
    "        logger.warning(\"No regex pattern found in the generated block:\\n {regex_block}\")\n",
    "        raise None\n",
    "    return pattern.group(1)\n",
    "\n",
    "pattern = \"匹配一个邮箱地址\"\n",
    "v = generate_regex_pattern.with_llm(llm).with_extractor(extract_regex_pattern).run(desc=pattern)\n",
    "print(v)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from byzerllm.types import Int, Bool,Float\n",
    "@byzerllm.prompt()\n",
    "def add(a: int, b:int) -> Int:\n",
    "    \"\"\"\n",
    "    你是一个加法计算器。给定两个数字,返回计算结果。\n",
    "    输入： \n",
    "    1. {{a}}    \n",
    "    2. {{b}}\n",
    "    \"\"\"  \n",
    "\n",
    "v = add.with_llm(llm).run(1,2)    \n",
    "type(v.value)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import byzerllm\n",
    "data = {\n",
    "    'name': 'Jane Doe',\n",
    "    'task_count': 3,\n",
    "    'tasks': [\n",
    "        {'name': 'Submit report', 'due_date': '2024-03-10'},\n",
    "        {'name': 'Finish project', 'due_date': '2024-03-15'},\n",
    "        {'name': 'Reply to emails', 'due_date': '2024-03-08'}\n",
    "    ]\n",
    "}\n",
    "\n",
    "\n",
    "class RAG():\n",
    "    def __init__(self):        \n",
    "        self.llm = byzerllm.ByzerLLM()\n",
    "        self.llm.setup_template(model=\"deepseek_chat\",template=\"auto\")\n",
    "        self.llm.setup_default_model_name(\"deepseek_chat\")        \n",
    "    \n",
    "    @byzerllm.prompt(lambda self:self.llm)\n",
    "    def generate_answer(self,name,task_count,tasks)->str:\n",
    "        '''\n",
    "        Hello {{ name }},\n",
    "\n",
    "        This is a reminder that you have {{ task_count }} pending tasks:\n",
    "        {% for task in tasks %}\n",
    "        - Task: {{ task.name }} | Due: {{ task.due_date }}\n",
    "        {% endfor %}\n",
    "\n",
    "        Best regards,\n",
    "        Your Reminder System\n",
    "        '''        \n",
    "\n",
    "t = RAG()\n",
    "\n",
    "response = t.generate_answer(**data)\n",
    "print(response)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import byzerllm\n",
    "\n",
    "data = {\n",
    "    'name': 'Jane Doe',\n",
    "    'task_count': 3,\n",
    "    'tasks': [\n",
    "        {'name': 'Submit report', 'due_date': '2024-03-10'},\n",
    "        {'name': 'Finish project', 'due_date': '2024-03-15'},\n",
    "        {'name': 'Reply to emails', 'due_date': '2024-03-08'}\n",
    "    ]\n",
    "}\n",
    "\n",
    "\n",
    "class RAG():\n",
    "    def __init__(self):        \n",
    "        self.llm = byzerllm.ByzerLLM.from_default_model(model=\"deepseek_chat\")\n",
    "    \n",
    "    @byzerllm.prompt()\n",
    "    def generate_answer(self,name,task_count,tasks)->str:\n",
    "        '''\n",
    "        Hello {{ name }},\n",
    "\n",
    "        This is a reminder that you have {{ task_count }} pending tasks:\n",
    "            \n",
    "        {{ tasks }}\n",
    "\n",
    "        Best regards,\n",
    "        Your Reminder System\n",
    "        '''\n",
    "        \n",
    "        tasks_str = \"\\n\".join([f\"- Task: {task['name']} | Due: { task['due_date'] }\" for task in tasks])\n",
    "        return {\"tasks\": tasks_str}\n",
    "\n",
    "t = RAG()\n",
    "\n",
    "response = t.generate_answer.with_llm(t.llm).run(**data)\n",
    "print(response)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pydantic\n",
    "class Time(pydantic.BaseModel):\n",
    "    time: str = pydantic.Field(...,description=\"时间，时间格式为 yyyy-MM-dd\")\n",
    "\n",
    "\n",
    "@llm.impl()\n",
    "def calculate_current_time()->Time:\n",
    "    '''\n",
    "    计算当前时间\n",
    "    '''\n",
    "    pass \n",
    "\n",
    "\n",
    "calculate_current_time()\n",
    "#output: Time(time='2024-06-14')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import byzerllm\n",
    "\n",
    "llm = byzerllm.ByzerLLM.from_default_model(model=\"deepseek_chat\")\n",
    "\n",
    "v = llm.stream_chat_oai(model=\"deepseek_chat\",conversations=[{\n",
    "    \"role\":\"user\",\n",
    "    \"content\":\"你好，你是谁\",\n",
    "}],delta_mode=True)\n",
    "\n",
    "for t in v:\n",
    "    print(t[0],flush=True)  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import byzerllm\n",
    "\n",
    "llm = byzerllm.ByzerLLM.from_default_model(model=\"deepseek_chat\")\n",
    "\n",
    "v = llm.chat_oai(model=\"deepseek_chat\",conversations=[{\n",
    "    \"role\":\"user\",\n",
    "    \"content\":\"你好，你是谁\",\n",
    "}])\n",
    "\n",
    "print(v[0].output)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List,Dict,Any,Annotated\n",
    "import pydantic \n",
    "import datetime\n",
    "from dateutil.relativedelta import relativedelta\n",
    "\n",
    "def compute_date_range(count:Annotated[int,\"时间跨度，数值类型\"],\n",
    "                       unit:Annotated[str,\"时间单位，字符串类型\",{\"enum\":[\"day\",\"week\",\"month\",\"year\"]}])->List[str]:\n",
    "    '''\n",
    "    计算日期范围\n",
    "\n",
    "    Args:\n",
    "        count: 时间跨度，数值类型\n",
    "        unit: 时间单位，字符串类型，可选值为 day,week,month,year\n",
    "    '''        \n",
    "    now = datetime.datetime.now()\n",
    "    now_str = now.strftime(\"%Y-%m-%d %H:%M:%S\")\n",
    "    if unit == \"day\":\n",
    "        return [(now - relativedelta(days=count)).strftime(\"%Y-%m-%d %H:%M:%S\"),now_str]\n",
    "    elif unit == \"week\":\n",
    "        return [(now - relativedelta(weeks=count)).strftime(\"%Y-%m-%d %H:%M:%S\"),now_str]\n",
    "    elif unit == \"month\":\n",
    "        return [(now - relativedelta(months=count)).strftime(\"%Y-%m-%d %H:%M:%S\"),now_str]\n",
    "    elif unit == \"year\":\n",
    "        return [(now - relativedelta(years=count)).strftime(\"%Y-%m-%d %H:%M:%S\"),now_str]\n",
    "    return [\"\",\"\"]\n",
    "\n",
    "def compute_now()->str:\n",
    "    '''\n",
    "    计算当前时间\n",
    "    '''\n",
    "    return datetime.datetime.now().strftime(\"%Y-%m-%d %H:%M:%S\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "t = llm.chat_oai([{\n",
    "    \"content\":'''计算当前时间''',\n",
    "    \"role\":\"user\"    \n",
    "}],tools=[compute_date_range,compute_now],execute_tool=True)\n",
    "\n",
    "t[0].values"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "t = llm.chat_oai([{\n",
    "    \"content\":'''最近三个月趋势''',\n",
    "    \"role\":\"user\"    \n",
    "}],tools=[compute_date_range,compute_now],execute_tool=True)\n",
    "\n",
    "t[0].values"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from byzerllm.types import Bool\n",
    "\n",
    "@byzerllm.prompt()\n",
    "def detect_rm_command(command: str) -> Bool:\n",
    "    '''\n",
    "    给定如下shell脚本：\n",
    "\n",
    "    ```shell\n",
    "    {{ command }}\n",
    "    ```\n",
    "\n",
    "    如果该脚本中包含rm命令，请返回True，否则返回False。\n",
    "    '''\n",
    "\n",
    "t = detect_rm_command.with_llm(llm).run(command=\"cd www\\nrm -rf /; cp -rf /\")\n",
    "print(t)\n",
    "\n",
    "t = detect_rm_command.with_llm(llm).run(command=\"cd www\\nmv -rf /; cp -rf /\")\n",
    "print(t)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "## image2text\n",
    "\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "byzerllm",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.11"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
